package com.ce.wnd;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.ce.po.NTree;
import com.ce.po.NTreeItem;
import com.ce.po.VChannelDO;
import com.ce.thd.InitTreeThread;
import com.ce.thd.WritePropertiesThread;

public class ExportWnd extends Composite {

	private Tree tree;
	private Text text;
	private List<VChannelDO> channels = new ArrayList<VChannelDO>();
	private int iCheck = 0;
	private ProgressBar progressBar;
	private NTree nTree;
	public ExportWnd(Composite parent, int style) {
		super(parent, style);
		setLayout(new FormLayout());

		progressBar = new ProgressBar(this, SWT.NONE);
		final FormData fd_progressBar = new FormData();
		fd_progressBar.top = new FormAttachment(100, -25);
		fd_progressBar.bottom = new FormAttachment(100, -7);
		fd_progressBar.right = new FormAttachment(100, -5);
		fd_progressBar.left = new FormAttachment(0, 5);
		progressBar.setLayoutData(fd_progressBar);

		text = new Text(this, SWT.BORDER);
		text.setText("E:/wypsmall/Code/Code_j2ee_e34/NewEtvSDExport/etvsd/exp.properties");
		final FormData fd_text = new FormData();
		fd_text.bottom = new FormAttachment(0, 28);
		fd_text.right = new FormAttachment(100, -5);
		fd_text.left = new FormAttachment(0, 5);
		fd_text.top = new FormAttachment(0, 10);
		text.setLayoutData(fd_text);

		tree = new Tree(this, SWT.BORDER | SWT.CHECK);
		tree.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (e.detail == SWT.CHECK) {  
                    TreeItem item = (TreeItem) e.item;  
                    NTreeItem nitem = findNTreeItem(item);
                    boolean state = item.getChecked();  
                    checkChildrenNode(item, state);  
                    checkParentsNode(item, state);
                    ncheckChildrenNode(nitem, state);  
                    ncheckParentsNode(nitem, state);
                }
			}
		});
		final FormData fd_tree = new FormData();
		fd_tree.top = new FormAttachment(0, 60);
		fd_tree.right = new FormAttachment(0, 205);
		fd_tree.bottom = new FormAttachment(100, -30);
		fd_tree.left = new FormAttachment(0, 5);
		tree.setLayoutData(fd_tree);

		Button btnInitTree;
		btnInitTree = new Button(this, SWT.NONE);
		btnInitTree.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try {
					initTree();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		final FormData fd_btnInitTree = new FormData();
		fd_btnInitTree.right = new FormAttachment(0, 53);
		fd_btnInitTree.bottom = new FormAttachment(0, 55);
		fd_btnInitTree.left = new FormAttachment(0, 5);
		fd_btnInitTree.top = new FormAttachment(0, 33);
		btnInitTree.setLayoutData(fd_btnInitTree);
		btnInitTree.setText("初始化");

		final Button btnWriteFile = new Button(this, SWT.NONE);
		btnWriteFile.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				iCheck = 0;
				travelTree(tree);
				System.out.println(iCheck);
				progressBar.setMinimum(0);
				progressBar.setMaximum(iCheck);
				String path = text.getText();
				WritePropertiesThread wth = new WritePropertiesThread(nTree,progressBar,Display.getDefault(),path);
				wth.start();

			}
		});
		final FormData fd_btnWriteFile = new FormData();
		fd_btnWriteFile.right = new FormAttachment(0, 106);
		fd_btnWriteFile.bottom = new FormAttachment(0, 55);
		fd_btnWriteFile.left = new FormAttachment(0, 58);
		fd_btnWriteFile.top = new FormAttachment(0, 33);
		btnWriteFile.setLayoutData(fd_btnWriteFile);
		btnWriteFile.setText("写文件");
	}
	
    protected NTreeItem findNTreeItem(TreeItem item) {
    	NTreeItem[] items = nTree.getItems();
    	NTreeItem sel = null;
    	for (NTreeItem treeItem : items) {
			if(treeItem.getData().equals(item.getData())){
				sel = treeItem;
				break;
			} else {
				sel = travelNTree(treeItem,item);
				if(sel != null)
					return sel;
				}
		}
		return sel;
	}
    
	private NTreeItem travelNTree(NTreeItem node, TreeItem item) {
		NTreeItem sel = null;
		if(node.getItemCount() == 0)
			return sel;
		NTreeItem[] items = node.getItems();
		for (int i = 0; i < items.length; i++) {
			if(items[i].getData().equals(item.getData())){
				sel = items[i];
				return sel;
			}
			sel = travelNTree(items[i],item);
			if(sel != null)
				return sel;
		}
		return sel;
	}

	private void checkParentsNode(TreeItem item, boolean state)  
    {  
        item.setChecked(state);  
        TreeItem parent = item.getParentItem();
        boolean pstate = state;
        if(parent != null) {
        	parent.setChecked(state);
        	TreeItem[] items = parent.getItems();
        	for (int i = 0; i < items.length; i++) {
				if(items[i].getChecked()) {
					pstate = true;
					parent.setChecked(pstate);
					break;
				}
			}
            checkParentsNode(parent, pstate);  
        }  
    } 

    private void checkChildrenNode(TreeItem item, boolean state)  
    {  
        item.setChecked(state);  
        TreeItem[] items = item.getItems();  
        for (TreeItem treeItem : items)  
        {  
            checkChildrenNode(treeItem, state);  
        }  
    } 
    
    private void ncheckParentsNode(NTreeItem item, boolean state)  
    {  
        item.setChecked(state);  
        NTreeItem parent = item.getParentItem();
        boolean pstate = state;
        if(parent != null) {
        	parent.setChecked(state);
        	NTreeItem[] items = parent.getItems();
        	for (int i = 0; i < items.length; i++) {
				if(items[i].getChecked()) {
					pstate = true;
					parent.setChecked(pstate);
					break;
				}
			}
            ncheckParentsNode(parent, pstate);  
        }  
    } 

    private void ncheckChildrenNode(NTreeItem item, boolean state)  
    {  
        item.setChecked(state);  
        NTreeItem[] items = item.getItems();  
        for (NTreeItem treeItem : items)  
        {  
            ncheckChildrenNode(treeItem, state);  
        }  
    }

	private void initTree() {
		channels.add(new VChannelDO("1", "电视剧"));
		channels.add(new VChannelDO("2", "电影"));
		channels.add(new VChannelDO("3", "综艺"));
		channels.add(new VChannelDO("4", "儿童"));
		channels.add(new VChannelDO("5", "音乐"));
		channels.add(new VChannelDO("6", "动漫"));
		nTree = new NTree();
		InitTreeThread initth = new InitTreeThread(channels,tree,nTree,Display.getDefault());
		initth.start();
	}
	
	private void travelTree(Object otree) {
		if(otree instanceof Tree) {
			Tree t = (Tree)otree;
			TreeItem[] items = t.getItems();
			for (int i = 0; i < items.length; i++) {
				if(items[i].getChecked()){
					iCheck++;
					travelTree(items[i]);
				}
			}
		}
		if(otree instanceof TreeItem) {
			TreeItem node = (TreeItem)otree;
			if(node.getChecked()) {
				if(node.getItemCount() == 0)
					return;
				TreeItem[] items = node.getItems();
				for (int i = 0; i < items.length; i++) {
					if(items[i].getChecked()){
						iCheck++;
						travelTree(items[i]);
					}
				}
			}
		}

	}
}
